<template>
  <el-dialog width="500px" :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="订单编号">
        <el-input v-model="form.orderId" disabled />
      </el-form-item>
      <el-form-item label="原因理由" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="4" placeholder="原因理由" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
  
<script>
import { rejectOrderApproval,cancelOrderApproval } from '@/api/order'
export default {
  // props: ['orderId'],
  props: {
    orderId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        orderId: '',
        remark: '',
      },
      rules: {
        remark: [
          { required: true, message: '驳回理由不能为空', trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.form.orderId = this.orderId
        this.form.remark = ''
      } else {
        this.$refs['ruleForm'].resetFields()
      }
    }
  },
  methods: {
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          let data = {
            orderId: this.form.orderId,
            reason: this.form.remark
          }
          if (this.title == '驳回理由') {
            rejectOrderApproval(data).then(res => {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.dialogVisible = false
              this.$emit('getList', true)
            })
          } else {
            cancelOrderApproval(data).then(res => {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.dialogVisible = false
              this.$emit('getList', true)
            })
          }

        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    },
  }
}
</script>
  
<style lang="scss" scoped></style>
  